package com.ls.lserp.modules.sys.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ls.lserp.common.utils.CacheUtils;
import com.ls.lserp.common.utils.SpringContextHolder;
import com.ls.lserp.dbaccess.Entity.QualityControlEntity;
import com.ls.lserp.dbaccess.Entity.QualityControlREntity;
import com.ls.lserp.modules.order.entity.Order;
import com.ls.lserp.modules.sys.dao.QualityDao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * 质检项目工具类
 *
 * @author T.S.Y
 * @version 2018-06-28
 */
public class QualityUtils {

	private static QualityDao qualityDao = SpringContextHolder.getBean(QualityDao.class);
	//质检基础数据key
	public static final String CACHE_QUALITY_VALUE_MAP = "qualityValueMap";
	//质检工序质检数据集key
	public static final String CACHE_QUALITY_OPERATING_MAP = "qualityOperatingMap";
	
	public static final String CACHE_QUALITY_VALUE_MAP_34 = "qualityValueMap34";
	public static final String CACHE_QUALITY_VALUE_MAP_35 = "qualityValueMap35";
	public static final String CACHE_QUALITY_VALUE_MAP_36 = "qualityValueMap36";
	public static final String CACHE_QUALITY_VALUE_MAP_37 = "qualityValueMap37";

	/**
	 * 放入缓存初始方法
	 */
	public static void putQualityCache() {
		putQcListByOperatingCache();
		putQcEntityCache();
	}
	
	/**
	 * 清除缓存方法
	 */
	public static void removeQualityCache() {
		CacheUtils.remove(CACHE_QUALITY_VALUE_MAP);
		CacheUtils.remove(CACHE_QUALITY_OPERATING_MAP);
	}
	
	/**
	 * 按照不同工序将质检数据集放入缓存中
	 * @return
	 */
	public static Map<String, List<QualityControlEntity>> putQcListByOperatingCache() {
		@SuppressWarnings("unchecked")
		// 获取缓存数据
		Map<String, List<QualityControlEntity>> qualityListMap = (Map<String, List<QualityControlEntity>>) CacheUtils.get(CACHE_QUALITY_OPERATING_MAP);
		// 若缓存数据不存在，则执行放入缓存操作
		if (qualityListMap == null) {
			qualityListMap = Maps.newHashMap();
			// 获取工序34质检数据集
			List<QualityControlEntity> qualityList_34 = getQCListByOpId(34L);
			// 获取工序35质检数据集
			List<QualityControlEntity> qualityList_35 = getQCListByOpId(35L);
			// 获取工序36质检数据集
			List<QualityControlEntity> qualityList_36 = getQCListByOpId(36L);
			// 获取工序37质检数据集
			List<QualityControlEntity> qualityList_37 = getQCListByOpId(37L);
			// 存入map集合
			qualityListMap.put("34", qualityList_34);
			qualityListMap.put("35", qualityList_35);
			qualityListMap.put("36", qualityList_36);
			qualityListMap.put("37", qualityList_37);
			// 放入缓存
			CacheUtils.put(CACHE_QUALITY_OPERATING_MAP, qualityListMap);
		}
		return qualityListMap;
	}
	
	/**
	 * 将质检基础数据放入缓存中
	 * @return
	 */
	public static Map<String, QualityControlEntity> putQcEntityCache() {
		@SuppressWarnings("unchecked")
		// 获取缓存数据
		Map<String, QualityControlEntity> qualityMap = (Map<String, QualityControlEntity>) CacheUtils.get(CACHE_QUALITY_VALUE_MAP);
		// 若缓存数据不存在，则执行放入缓存操作
		if (qualityMap == null) {
			qualityMap = Maps.newHashMap();
			for (QualityControlEntity qEntity : findAllList(new QualityControlEntity())) {
					//存入map集合
					qualityMap.put(qEntity.getQcItemCd() + "-" + qEntity.getQcItemValue(), qEntity);
			}
			// 放入缓存
			CacheUtils.put(CACHE_QUALITY_VALUE_MAP, qualityMap);
		}
		return qualityMap;
	}
	
	/**
	 * 获取全部质检基础数据
	 * @param qualityControlEntity
	 * @return
	 */
	public static List<QualityControlEntity> findAllList(QualityControlEntity qualityControlEntity) {
		return qualityDao.findAllList(qualityControlEntity);
	}
	
	/**
	 * 按照工序id获取质检工序关联数据
	 * @param operatingId
	 * @return
	 */
	public static List<QualityControlREntity> findQCOperatingByOpId(Long operatingId) {
		return qualityDao.findQCOperatingByOpId(operatingId);
	}
	
	/**
	 * 根据工序id获取质检项目数据集
	 * @param operatingId
	 * @return
	 */
	public static List<QualityControlEntity> getQCListByOpId(Long operatingId) {
		List<QualityControlEntity> qualityList = new ArrayList<QualityControlEntity>();
		// 获取全部质检基础数据
		List<QualityControlEntity> qualityAllList = findAllList(new QualityControlEntity());
		// 按照工序id获取质检工序关联数据
		List<QualityControlREntity> qcOperatingList = findQCOperatingByOpId(operatingId);
		// 根据质检关联数据筛选质检数据
		if (qcOperatingList != null && qcOperatingList.size() > 0) {
			for (QualityControlREntity qcR : qcOperatingList) {
				for (QualityControlEntity qc : qualityAllList) {
					if (qcR.getQcId() == qc.getQcId()) {
						qualityList.add(qc);
					}
				}
			}
		}
		// 返回质检数据集
		return qualityList;
	}
	
	/**
	 * 返回质检项目名称
	 * 
	 * @param value
	 * @return
	 */
	public static String getQualityItemName(String value) {
		// 获取缓存数据
		Map<String, QualityControlEntity> qcEntityMap = putQcEntityCache();
		// 通过key获取value
		QualityControlEntity returnQuality = qcEntityMap.get(value);
		// 返回数据
		if (returnQuality != null) {
			return returnQuality.getQcItem();
		} else {
			return "";
		}
	}
	
	/**
	 * 返回质检数据集
	 * 
	 * @param value
	 * @return
	 */
	public static List<QualityControlEntity> getQualityListByOpId(String operatingId) {
		// 获取缓存数据
		Map<String, List<QualityControlEntity>> qcEntityMap = putQcListByOperatingCache();
		// 根据工序id获取相应质检数据集
		List<QualityControlEntity> returnQualityList = qcEntityMap.get(operatingId);
		// 返回数据
		if (returnQualityList != null) {
			return returnQualityList;
		} else {
			return new ArrayList<QualityControlEntity>();
		}
	}
	
	/**
	 * @Title: putQualityCache
	 * @Description: 质检项目全量放入缓存
	 * @author 于辉
	 *
	 */
	public static void putQualityCache1() {
		@SuppressWarnings("unchecked")
		Map<String, List<Order>> qualityMap34 = (Map<String, List<Order>>) CacheUtils.get(CACHE_QUALITY_VALUE_MAP_34);
		if (qualityMap34 == null) {
			qualityMap34 = Maps.newHashMap();
			for (Order order : findAllList(new Order(),34L)) {
				List<Order> dictList = qualityMap34.get(order.getStyleBarCode());
				if (dictList != null) {
					dictList.add(order);
					qualityMap34.put(order.getStyleBarCode(),  Lists.newArrayList(dictList));
				} else {
					qualityMap34.put(order.getStyleBarCode(), Lists.newArrayList(order));
				}
				
			}
			CacheUtils.put(CACHE_QUALITY_VALUE_MAP_34, qualityMap34);
		}
		@SuppressWarnings("unchecked")
		Map<String, List<Order>> qualityMap35 = (Map<String, List<Order>>) CacheUtils.get(CACHE_QUALITY_VALUE_MAP_35);
		if (qualityMap35 == null) {
			qualityMap35 = Maps.newHashMap();
			for (Order order : findAllList(new Order(),35L)) {
				List<Order> dictList = qualityMap35.get(order.getStyleBarCode());
				if (dictList != null) {
					dictList.add(order);
					qualityMap35.put(order.getStyleBarCode(),  Lists.newArrayList(dictList));
				} else {
					qualityMap35.put(order.getStyleBarCode(), Lists.newArrayList(order));
				}
				
			}
			CacheUtils.put(CACHE_QUALITY_VALUE_MAP_35, qualityMap35);
		}
		@SuppressWarnings("unchecked")
		Map<String, List<Order>> qualityMap36 = (Map<String, List<Order>>) CacheUtils.get(CACHE_QUALITY_VALUE_MAP_36);
		if (qualityMap36 == null) {
			qualityMap36 = Maps.newHashMap();
			for (Order order : findAllList(new Order(),36L)) {
				List<Order> dictList = qualityMap36.get(order.getStyleBarCode());
				if (dictList != null) {
					dictList.add(order);
					qualityMap36.put(order.getStyleBarCode(),  Lists.newArrayList(dictList));
				} else {
					qualityMap36.put(order.getStyleBarCode(), Lists.newArrayList(order));
				}
				
			}
			CacheUtils.put(CACHE_QUALITY_VALUE_MAP_36, qualityMap36);
		}
		@SuppressWarnings("unchecked")
		Map<String, List<Order>> qualityMap37 = (Map<String, List<Order>>) CacheUtils.get(CACHE_QUALITY_VALUE_MAP_37);
		if (qualityMap37 == null) {
			qualityMap37 = Maps.newHashMap();
			for (Order order : findAllList(new Order(),37L)) {
				List<Order> dictList = qualityMap37.get(order.getStyleBarCode());
				if (dictList != null) {
					dictList.add(order);
					qualityMap37.put(order.getStyleBarCode(),  Lists.newArrayList(dictList));
				} else {
					qualityMap37.put(order.getStyleBarCode(), Lists.newArrayList(order));
				}
				
			}
			CacheUtils.put(CACHE_QUALITY_VALUE_MAP_37, qualityMap37);
		}
	}
	
	/**
     * 获取订单基本信息
     * @param 
     * @return
     */
    public static List<Order> findAllList(Order order,Long operatingId){
    	if (operatingId!=null) {
    		order.setOperatingId(operatingId);
    		List<Order> orders = qualityDao.findAllOrderList(order);
    		for (Order orz:orders) {
    			if (orz!=null) {
    				orz.setStyleOpList(qualityDao.findStyleControl(orz.getStyleNo(),operatingId+""));
    			}
    		}
    		return orders;
		}
		return null;
    }
}
